<html>
<head>

<title>Groovy Goodness: See if List and Object Array are Equal</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: See if List and Object Array are Equal</h3>

<div class="post">
<p>Groovy adds a lot of useful methods to standard JDK classes. For example Groovy adds the <code>equals()</code> method to <code>List</code> and <code>Object[]</code> so both can be compared. We must make sure the array is of type <code>Object[]</code> to make it work. Also the <code>equals()</code> method is added to arrays of type <code>int</code>.</p>
<pre class="brush:groovy">
def numbers1 = [1,2,3] as int[]
def numbers2 = [1,2,3] as int[]
def numbers3 = [1,2] as int[]

assert numbers1.equals(numbers2)
assert numbers2 == numbers1
assert !(numbers1.equals(numbers3))

def list = ['Groovy', 'Grails', 'Gradle']
def stringArray1 = ['Grails', 'Gradle', 'Groovy'] as Object[]
def stringArray2 = ['Groovy', 'Grails', 'Gradle'] as Object[]

assert list.equals(stringArray2)
assert list == stringArray2
assert !(list.equals(stringArray1)) // order matters
assert list != stringArray1
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>